$(document).ready(function(){
	var contextPath = document.location.pathname;
	var index = contextPath.substr(1).indexOf("/");
	contextPath = contextPath.substr(1, index);
	var basePath = "/" + contextPath;
	initProvince();
	var province = null;
	var city = null;
	var area = null;
	/**定时器定时重新获取短信验证码*/
	var second = 90;
	var time = 1000; 
	var interval; //调度器对象。
	var temp = 0;
	/**
	 * 修改手机号码
	 */
	var validObject = $("#changePhoneForm").Validform({

		dataType : 'json',
		tiptype : 2,
		type : "POST",
		ajaxPost : true,
		callback : function(data) {
			if (data.flag == 0) {
				$("#phoneInfo").modal('hide');
				window.location.assign(basePath + "/HRTWEB/personalCenter/reset.jsp");
				
			} else {
				$("#phoneInfo").modal('hide');
				alert("验证码错误!");
				
			}
		},

		tiptype : function(msg, o, cssctl) {

			if (!o.obj.is("form")) {
				var tds = o.obj.text();

				var objtip = o.obj.siblings(".Validform_checktip");
				cssctl(objtip, o.type);
				objtip.text(msg);

			}

		}
	});
	
	/**
	 * 修改邮件
	 */
	var mailObject = $("#changeMailForm").Validform({

		dataType : 'json',
		tiptype : 2,
		type : "POST",
		ajaxPost : true,
		callback : function(data) {
			if (data.flag == 0) {
				$("#EmailInfo").modal('hide');
				window.location.assign(basePath + "/HRTWEB/personalCenter/reset.jsp");
				
			} else {
				$("#EmailInfo").modal('hide');
				alert("验证码错误!");
				
			}
		},

		tiptype : function(msg, o, cssctl) {

			if (!o.obj.is("form")) {
				var tds = o.obj.text();

				var objtip = o.obj.siblings(".Validform_checktip");
				cssctl(objtip, o.type);
				objtip.text(msg);

			}

		}
	});
	
	
	/**
	 * 手机号码验证之后调用的方法
	 */
	$("#changePhoneForm").delegate('#mobile', 'change', function() {
		validObject.config({
		 ajaxurl:{
		        success:function(data,obj){
		        	if(data.status=="y"){
		        		$("#validateBtn").removeAttr("disabled");
		        	}else{
		        		$("#validateBtn").attr("disabled","true");
		        	}
		        	
		           
		        },
		    	 error:function(data,obj){
			            alert("error:"+data+obj);
			        }
		        
		    }
			
			
		});
	
	});
	
	
	/**
	 * 邮件验证之后调用的方法
	 */
	$("#changeMailForm").delegate('#mail', 'change', function() {
		mailObject.config({
		 ajaxurl:{
		        success:function(data,obj){
		        	if(data.status=="y"){
		        		$("#getMailCodeBtn").removeAttr("disabled");
		        	}else{
		        		$("#getMailCodeBtn").attr("disabled","true");
		        	}
		        	
		           
		        },
		    	 error:function(data,obj){
			            alert("error:"+data+obj);
			        }
		        
		    }
			
			
		});
	
	});
	
	

	/**
	 * 获取手机验证码
	 */
	$("#changePhoneForm").delegate(
			'#validateBtn',
			'click',
			function(){
				alert("validateBtn");
				getMobile();
	});	
	
	/**
	 * 获取邮箱验证码
	 */
	$("#changeMailForm").delegate(
			'#getMailCodeBtn',
			'click',
			function(){
				alert("getMailCodeBtn");
				getMail();
	});	
	/**
	 * 获取邮箱验证码
	 */
	function getMail(){
		var mail = $("#mail").val();
		$("#tipeMail").css("display","block");
		$("#getMailCodeBtn").css("display","none");
	    var memberId = $("#memberId").val();
	    temp = 1;
	    run();
		var saveUrl = basePath+"/HRTWEB/mailRecord/save.do";
		$.ajax({
					url : saveUrl,
					dataType : 'text',
					data : {
						mail:mail,
						memberId:memberId
					},
					cache : false,

					success : function(data) {
						if(data=="success"){
							alert("邮箱验证码发送成功！！");
						}
					},
					error : function(html) {
						alert("邮箱验证码发送失败！！");
						return;
					}
				});

	}
	
function getMobile(){
		
		var mobile = $("#mobile").val();
		$("#tipeMobile").css("display","block");
		$("#validateBtn").css("display","none");
	    var memberId = $("#memberId").val();
	    temp = 0;
	    run();
		var saveUrl = basePath+"/HRTWEB/smsRecord/save.do";
		$.ajax({
					url : saveUrl,
					dataType : 'text',
					data : {
						mobile:mobile,
						memberId:memberId
					},
					cache : false,

					success : function(data) {
						if(data=="success"){
							alert("验证码发送成功！！");
						}
					},
					error : function(html) {
						alert("验证码发送失败！！");
						return;
					}
				});

	}
	
	function run(){
		interval = setInterval(fun,time);//去掉引号就对了
		}
	function fun(){
		if(temp == 0){
			if(second>=0){
				$("#codeTipeClock1").text(second+"秒后重新获取");
				second--;
			}else{
				$("#validateBtn").text("重新获取短信验证码");
				$("#codeTipeClock1").text("");
				$("#validateBtn").css("display","block");
				$("#tipeMobile").css("display","none");
				clearTimeout(interval);
				second=90;
				}
		}else{
			if(second>=0){
				$("#codeTipeClock2").text(second+"秒后重新获取");
				second--;
			}else{
				$("#getMailCodeBtn").text("重新获取短信验证码");
				$("#codeTipeClock2").text("");
				$("#getMailCodeBtn").css("display","block");
				$("#tipeMail").css("display","none");
				clearTimeout(interval);
				second=90;
				}
		}
	}

	
	
	/**
	 * 初始化省份
	 */
	function initProvince(){
		 var seachprov = $("#seachprov");
			var editUrl = basePath+"/HRTWEB/address/province/list.do";
			$.ajax({

				url : editUrl,
				dataType : 'json',
				cache : false,

				success : function(data) {
					var results = data.provinceList;
					
					var rowContent = "<option  value ='-1' >请选择</option>";
					if(results!=null){
						
						for(i=0;i<results.length;i++){
							var province =results[i];
							rowContent = rowContent+"<option  value ="+province.code+" >"+province.name+"</option>";
						}
					}
					seachprov.append(rowContent);
				},
				error : function(html) {
					alert("Load Data failly");
					refresh();
				}
			});
	}
	
});



function pickDate(){
	laydate({
        elem: '#myborth',
        format: 'YYYY-MM-DD',
        festival: true, //显示节日
    });
}





function changeComplexProvince(provincecode) {
		$("#seachcity").empty(); //改变省份时先清空原先的市
		$("#seachdistrict").empty();
		var seachcity = $("#seachcity");
		province = $("#seachprov option:selected").text();
		
		var editUrl ="../address/city/list.do?";
		$.ajax({

			url : editUrl,
			dataType : 'json',
			cache : false,
			data : {
				provincecode : provincecode
			},
			success : function(data) {
				var results = data.cityList;
				
				var rowContent = "<option  value ='-1' >请选择</option>";
				if(results!=null){
					
					for(i=0;i<results.length;i++){
						var city =results[i];
						rowContent = rowContent+"<option  value ="+city.code+" >"+city.name+"</option>";
					}
				}
				seachcity.append(rowContent);
			},
			error : function(html) {
				alert("Load Data failly");
				refresh();
			}
		});
	
  
}

 
function changeCity(citycode) {
	
	$("#seachdistrict").empty(); //改变城市时先删除原先的区
	var seachdistrict = $("#seachdistrict");
	
	city = $("#seachcity option:selected").text();
	
	
	var editUrl ="../address/area/list.do?";
	$.ajax({

		url : editUrl,
		dataType : 'json',
		cache : false,
		data : {
			citycode : citycode
		},
		success : function(data) {
			var results = data.areaList;
			
			var rowContent = "<option  value ='-1' >请选择</option>";
			if(results!=null){
				
				for(i=0;i<results.length;i++){
					var area =results[i];
					rowContent = rowContent+"<option  value ="+area.code+" >"+area.name+"</option>";
				}
			}
			seachdistrict.append(rowContent);
		},
		error : function(html) {
			alert("Load Data failly");
			refresh();
		}
	});
    
}
function changeArea(areacode){
	area = $("#seachdistrict option:selected").text();
	
	var address = province+city+area;
	
	$("#address").val(address);
	
}


function clearAddress() {
	$("#seachprov").empty(); 
	$("#seachcity").empty(); 
	$("#seachdistrict").empty(); 
}
